﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    // NOTE: If you change the class name "Shift" here, you must also update the reference to "Shift" in App.config.
    public partial class MainService : IShift
    {
        public int Shift_Using(int ShiftID)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Shift_Using", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@ShiftID", ShiftID);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet Shift_GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Shift_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet Shift_GetAllByDepartment(int DepartmentID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Shift_GetAllByDepartmentID", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@DepartmentID", DepartmentID);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet Shift_GetList(out int ItemCount, int currentPage, int pageSize, int DeptID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Shift_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            dbCmd.Parameters.AddWithValue("@DepartmentID", DeptID);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public ShiftInfo Shift_GetInfo(int ShiftID)
        {
            ShiftInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Shift_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Shift_ID", ShiftID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new ShiftInfo();
                    retVal.ID = Convert.ToInt32(dr["Shift_ID"]);
                    retVal.DepartmentID = Convert.ToInt32(dr["Shift_DepartmentID"]);
                    retVal.Name = dr["Shift_Name"].ToString();
                    retVal.TimeIn = dr["Shift_TimeIn"].ToString();
                    retVal.TimeOut = dr["Shift_TimeOut"].ToString();
                    retVal.TimeInB = dr["Shift_TimeInB"].ToString();
                    retVal.TimeOutB = dr["Shift_TimeOutB"].ToString();
                    retVal.Description = dr["Shift_Description"].ToString();
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int Shift_Insert(ShiftInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Shift_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Shift_DepartmentID", eInfo.DepartmentID);
            dbCmd.Parameters.AddWithValue("@Shift_Name", eInfo.Name);
            dbCmd.Parameters.AddWithValue("@Shift_TimeIn", eInfo.TimeIn);
            dbCmd.Parameters.AddWithValue("@Shift_TimeOut", eInfo.TimeOut);
            dbCmd.Parameters.AddWithValue("@Shift_TimeInB", eInfo.TimeInB);
            dbCmd.Parameters.AddWithValue("@Shift_TimeOutB", eInfo.TimeOutB);
            dbCmd.Parameters.AddWithValue("@Shift_Description", eInfo.Description);

            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool Shift_Update(ShiftInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Shift_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Shift_ID", eInfo.ID);
            dbCmd.Parameters.AddWithValue("@Shift_DepartmentID", eInfo.DepartmentID);
            dbCmd.Parameters.AddWithValue("@Shift_Name", eInfo.Name);
            dbCmd.Parameters.AddWithValue("@Shift_TimeIn", eInfo.TimeIn);
            dbCmd.Parameters.AddWithValue("@Shift_TimeOut", eInfo.TimeOut);
            dbCmd.Parameters.AddWithValue("@Shift_TimeInB", eInfo.TimeInB);
            dbCmd.Parameters.AddWithValue("@Shift_TimeOutB", eInfo.TimeOutB);
            dbCmd.Parameters.AddWithValue("@Shift_Description", eInfo.Description);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool Shift_Delete(int ShiftID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Shift_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Shift_ID", ShiftID);

            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}
